//
//  ViewController.m
//  HomeWork_UI_16
//
//  Created by 李劲成 on 16/3/31.
//  Copyright © 2016年 ljc. All rights reserved.
//

#import "ViewController.h"
#import "WaterCollectionView.h"
#import "UIImageView+WebCache.h"
#import "WaterFlowLayout.h"
#import "CellOfWater.h"
#import "Model.h"
#import "JSON.h"

@interface ViewController ()<WaterFlowLayoutDelegate, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) WaterFlowLayout *layout;

@property (nonatomic, retain) WaterCollectionView *collection;

@property (nonatomic, retain) NSMutableArray *arrOfModels;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadData];
    [self createLayout];
    [self createCollection];
    // Do any additional setup after loading the view, typically from a nib.

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 创建图层
- (void)createLayout {
    // 由于瀑布流是一种布局, 并不涉及布局内的内容, 所以需替换系统给定布局, 然后完成数据加载到布局的相应位置即可.
    
    self.layout = [[WaterFlowLayout alloc]init];
    // delegate方式传回等比缩放图片高度.
    self.layout.delegate = self;
    
    // 给定瀑布流每一列宽度
    CGFloat w = ([UIScreen mainScreen].bounds.size.width - 40) / 3;
    // 设置item大小
    self.layout.itemSize = CGSizeMake(w, w);
    // 设置瀑布流整体内容部分距四周宽度
    self.layout.sectionInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    // 设置相邻两列间距
    self.layout.lineSpacing = 10;
    // 瀑布流 3列
    self.layout.numberOfColumn = 3;

    [_layout release];

}

/* 实现协议方法, 等比缩放原图, 计算出高度值 */
- (CGFloat)heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    Model *model = self.arrOfModels[indexPath.item];
    
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - 40) / 3;
    
    CGFloat height = (model.height / model.width) * width;
    
    return height;

}
#pragma mark - 处理数据
- (void)loadData{
    id result = [JSON JSONWithName:@"Data" type:@"json"];
    self.arrOfModels = [NSMutableArray array];
    for (NSDictionary *dic in result) {
            Model *model = [[Model alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrOfModels addObject:model];
        
    }

}


#pragma mark - 创建collection
- (void)createCollection {
    self.collection = [[WaterCollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:_layout];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.view addSubview:self.collection];
    
    [self.collection registerClass:[CellOfWater class] forCellWithReuseIdentifier:@"CELL"];


}

/* 设置cell数目 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arrOfModels.count;
}
/* 设置每个cell样式, 并通过开源框架SDWebImage根据URL加载图片 */
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CellOfWater *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CELL" forIndexPath:indexPath];
    
    Model *mode = self.arrOfModels[indexPath.item];
    cell.backgroundColor = [UIColor yellowColor];
    [cell.ImageView sd_setImageWithURL:[NSURL URLWithString:mode.thumbURL]];

    return cell;
}

@end
